package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.service.INursingProjectService;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 护理项目Service业务层处理
 *
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        try {
            redisTemplate.delete(CacheConstants.NURSING_PROJECT_ENABLE_KEY);
        } catch (Exception e) {
            log.error("redis服务异常");
        }
        return nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        try {
            redisTemplate.delete(CacheConstants.NURSING_PROJECT_ENABLE_KEY);
        } catch (Exception e) {
            log.error("redis服务异常");
        }
        return nursingProjectMapper.updateById(nursingProject);
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingProjectByIds(Long[] ids) {
        try {
            redisTemplate.delete(CacheConstants.NURSING_PROJECT_ENABLE_KEY);
        } catch (Exception e) {
            log.error("redis服务异常");
        }
        return nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        try {
            redisTemplate.delete(CacheConstants.NURSING_PROJECT_ENABLE_KEY);
        } catch (Exception e) {
            log.error("redis服务异常");
        }

        return nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有启用的护理项目
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getEnableProject() {
        List<NursingProjectVo> list = null;
        try {
            //从redis中查询
            log.warn("从redis中查询到护理项目数据");
            list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_ENABLE_KEY);
        } catch (Exception e) {
            log.error("redis服务异常");
        }

        if (CollUtil.isNotEmpty(list)) {
            //从redis中查询启用的护理项目
            return list;
        }

        //从数据库中查
        log.warn("从数据库中查询到护理项目数据");
        list = nursingProjectMapper.getEnableProject();
        try {
            //将数据存入redis中,有效期一天
            redisTemplate.opsForValue().set(CacheConstants.NURSING_PROJECT_ENABLE_KEY, list, 60 * 60 * 24, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("redis服务异常");
        }
        return list;
    }

    @Override
    public List<NursingProject> selectEnableProjectList(NursingProject nursingProject) {
        nursingProject.setStatus(1);
        return selectNursingProjectList(nursingProject);
    }
}
